package com.sm.li.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.github.pagehelper.PageInfo;
import com.sm.li.common.page.PageBean;
import com.sm.li.dto.MenuDTO;
import com.sm.li.entity.Menu;
import com.sm.li.mapper.MenuMapper;
import com.sm.li.query.MenuQuery;
import com.sm.li.service.MenuService;
import com.sm.li.service.base.impl.BaseServiceImpl;
import com.sm.li.transfer.MenuTransfer;
import com.sm.li.utils.TreeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import sun.reflect.generics.tree.Tree;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

//两个实体类的属性值互换   BeanUtils jar包     spring提供了BeanUtils
@Service
@Transactional
public class MenuServiceImpl extends BaseServiceImpl<Menu> implements MenuService {

    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private MenuTransfer menuTransfer;

    @Override
    public PageBean<MenuDTO> searchPage(MenuQuery menuQuery) {
        if (menuQuery.isQuery()) {
            //是查询
            LambdaQueryWrapper<Menu> lambda = new QueryWrapper<Menu>().lambda();
            if (!StringUtils.isEmpty(menuQuery.getMenuTitle()))
                lambda.like(Menu::getMenuTitle, menuQuery.getMenuTitle());
            if (Objects.nonNull(menuQuery.getMenuType()))
                lambda.eq(Menu::getMenuType, menuQuery.getMenuType());
            if (Objects.nonNull(menuQuery.getStarTime()) && Objects.nonNull(menuQuery.getEndTime()))
                lambda.between(Menu::getCreateTime, menuQuery.getStarTime(), menuQuery.getEndTime());

            List<Menu> menus = menuMapper.selectList(lambda);
            PageInfo<Menu> pageInfo = new PageInfo<>(menus);
            List<MenuDTO> menuDTOS = menuTransfer.toDTO(menus);
            return PageBean.initData(pageInfo.getTotal(), menuDTOS);
        } else {
            //不是查询
            //查询所有一级分类
            List<Menu> rootMenus = menuMapper.selectList(new QueryWrapper<Menu>().lambda().eq(Menu::getParentId, 0L));
            PageInfo<Menu> pageInfo = new PageInfo<>(rootMenus);
            List<MenuDTO> rootDtoS = menuTransfer.toDTO(rootMenus);
            List<Menu> otherMenus = menuMapper.selectList(new QueryWrapper<Menu>().lambda().ne(Menu::getParentId, 0L));
            List<MenuDTO> otherDTOS = menuTransfer.toDTO(otherMenus);
//            rootDtoS.forEach(menuDTO -> getChildren(menuDTO, otherDTOS));
            TreeUtils.buildTreeData(rootDtoS,otherDTOS);
            return PageBean.initData(pageInfo.getTotal(), rootDtoS);
        }
    }

    @Override
    public List<MenuDTO> getTreeData() {
        List<Menu> rootMenus = menuMapper.selectList(new QueryWrapper<Menu>().lambda().eq(Menu::getParentId, 0L));
//        PageInfo<Menu> pageInfo = new PageInfo<>(rootMenus);
        List<MenuDTO> rootDtoS = menuTransfer.toDTO(rootMenus);
        List<Menu> otherMenus = menuMapper.selectList(new QueryWrapper<Menu>().lambda().ne(Menu::getParentId, 0L));
        List<MenuDTO> otherDTOS = menuTransfer.toDTO(otherMenus);
//        rootDtoS.forEach(menuDTO -> getChildren(menuDTO, otherDTOS));
        TreeUtils.buildTreeData(rootDtoS,otherDTOS);
        return rootDtoS;
    }

    //级联删除
    @Override
    public int cascadeDeleteChildren(Long id) {

//        List<Menu> menus = menuMapper.selectList(new QueryWrapper<Menu>().lambda().eq(Menu::getParentId, id));
        List<Long> ids = new ArrayList<>();
        getCascadeChildrenIds(id,ids);
        if (ids.size()>0){
            menuMapper.deleteBatchIds(ids);
        }
        return this.deleteById(id);
    }



//    //通过id获取下级id
//    public void getCascadeChildrenId(Long id, List<Long> ids) {
//        List<Menu> menus = menuMapper.selectList(new QueryWrapper<Menu>().lambda().eq(Menu::getParentId, id));
//        List<Long> childrenIds = menus.stream().map(Menu::getId).collect(Collectors.toList());
//        if (!CollectionUtils.isEmpty(childrenIds)) {
//            ids.addAll(childrenIds);
//            childrenIds.forEach(childrenId -> getCascadeChildrenId(childrenId, ids));
//        }
//
//    }

//    public void getChildren(MenuDTO menu, List<MenuDTO> list) {
//        List<MenuDTO> collect = list.stream().filter(menu1 -> menu1.getParentId().longValue() == menu.getId()).collect(Collectors.toList());
//        if (!CollectionUtils.isEmpty(collect)) {
//            menu.setChildren(collect);
//            collect.forEach(menu2 -> getChildren(menu2, list));
//        }
//    }

}
